package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Log;
import com.qf.model.Massage;
import com.qf.model.PageBean;
import com.qf.model.User;
import com.qf.model.pojo.Userloans;
import com.qf.service.LogService;
import com.qf.service.UserService;
import com.qf.service.impl.LogServiceImpl;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/log")
public class LogServlet extends BascServlet  {
    private LogService logService=new LogServiceImpl();
    private UserService userService=new UserServiceImpl();
    public void findAll(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        int uid=Integer.parseInt(req.getParameter("uid"));
        int page = Integer.parseInt(req.getParameter("page"));
        int pagesize =Integer.parseInt( req.getParameter("pagesize"));


        PageBean<Log> pageBean=new PageBean<>();
        pageBean.setCurPage(page);
        pageBean.setPageSize(pagesize);


        User user = userService.findByUid(uid);
        List<Log> all=null;
        if(user.getU_manager()==0){
            //普通用户
            all= logService.findAll(pageBean, user.getU_idcard(), 0);
        }else {
            //管理员
            all= logService.findAll(pageBean, user.getU_idcard(), 1);
        }


        pageBean.setData(all);

        if(pageBean.getCount()%pageBean.getPageSize()==0){
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize());
        }else {
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize()+1);
        }
        Massage<PageBean<Log>> m=new Massage<>(1,"查询成功",pageBean);
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }
}
